package com.tools.cleanmaster.bean;

import com.tools.cleanmaster.model.item.AppTrashItem;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by liyunfeng on 15/12/1.
 * 专清App 垃圾结果类
 */
public class AppTrash {
    private static final String TAG = "AppTrash";

    private String mAppName;

    /**
     * 应用垃圾文件数据
     */
    private List<AppTrashItem> mCacheTrashList = new ArrayList<AppTrashItem>();

    /**
     * 应用多媒体文件数据
     * key为 媒体类型分类 {@link FileType}
     */
    private Map<Integer, List<DeepTrash>> mDeepTrashMap = new HashMap<Integer, List<DeepTrash>>();

    private AtomicBoolean mIsSort = new AtomicBoolean(false);
    private String mPkgName;

    public AppTrash(String pkgName) {
        mPkgName = pkgName;
    }

    /**
     * 设置 应用名称
     */
    public void setAppName(String appName) {
        mAppName = appName;
    }

    /**
     * 获得 应用包名
     */
    public String getAppName() {
        return mAppName;
    }

    /**
     * 添加 快速扫描垃圾项
     */
    public void addQuickTrash(AppTrashItem appTrashItem) {
        mCacheTrashList.add(appTrashItem);
    }

    /**
     * 添加 深度细化分类的垃圾项
     *
     * @param type
     * @param deepTrash
     */
    public void addDeepTrash(int type, DeepTrash deepTrash) {
        List<DeepTrash> deepTrashList = mDeepTrashMap.get(type);
        if (null == deepTrashList) {
            deepTrashList = new ArrayList<DeepTrash>();
            mDeepTrashMap.put(type, deepTrashList);
        }
        deepTrashList.add(deepTrash);
    }

    //获得缓冲垃圾项 list
    public List<AppTrashItem> getCacheTrashList() {
        return new ArrayList<AppTrashItem>(mCacheTrashList);
    }

    //删除 清理项
    public void removeCacheTrashList(List<AppTrashItem> cacheTrashList) {
        if (null != mCacheTrashList) {
            mCacheTrashList.removeAll(cacheTrashList);
        }
    }

    //获取 所对类型的 垃圾子项list
    public List<DeepTrashItem> getDeepTrashItemsByType(int type) {
        if (null == mDeepTrashMap) {
            return null;
        }
        List<DeepTrash> trashItemList = mDeepTrashMap.get(type);
        if (null == trashItemList || trashItemList.isEmpty()) {
            return null;
        }
        List<DeepTrashItem> mResult = new ArrayList<DeepTrashItem>();
        for (DeepTrash item : trashItemList) {
            mResult.addAll(item.mSunList);
        }
        return mResult;
    }

    //获取 所对类型 多媒体垃圾信息list
    public List<DeepTrash> getDeepTrashs(int type) {
        if (null == mDeepTrashMap) {
            return Collections.emptyList();
        }
        List<DeepTrash> deepTrashList = mDeepTrashMap.get(type);
        if (null == deepTrashList) {
            return Collections.emptyList();
        }
        return deepTrashList;
    }

    //删除 垃圾子项list
    public void removeDeepTrashItemsByType(int type, List<DeepTrashItem> deepTrashItemList) {
        if (null == mDeepTrashMap) {
            return;
        }
        List<DeepTrash> trashItemList = mDeepTrashMap.get(type);
        if (null == trashItemList || trashItemList.isEmpty()) {
            return;
        }
        for (DeepTrash item : trashItemList) {
            item.mSunList.removeAll(deepTrashItemList);
        }
    }

    /**
     * 根据文件类型获取大小, 单位byte
     */
    public long getMediaSize(int type) {
        long size = 0;
        if (mDeepTrashMap == null) {
            return size;
        }
        List<DeepTrash> deepTrashList = mDeepTrashMap.get(type);
        if (deepTrashList == null || deepTrashList.isEmpty()) {
            return size;
        }
        for (DeepTrash trash : deepTrashList) {
            if (trash == null) {
                continue;
            }
            Collection<DeepTrashItem> itemColl = trash.mSunList;
            if (itemColl == null || itemColl.isEmpty()) {
                continue;
            }
            for (DeepTrashItem item : itemColl) {
                size += item.mSize;
            }
        }
        return size;
    }

    /**
     * 获取缓存大小, 单位byte
     */
    public long getCacheSize() {
        long size = 0;
        if (mCacheTrashList == null || mCacheTrashList.isEmpty()) {
            return size;
        }
        for (AppTrashItem item : mCacheTrashList) {
            size += item.size;
        }
        return size;
    }

    /**
     * 获取总大小, 单位byte
     */
    public long getTotalSize() {
        long size = 0;
        size += getCacheSize();
        for (int t : FileType.getFileTypeArray()) {
            size += getMediaSize(t);
        }
        return size;
    }

    public void clearCacheInfo() {
        mCacheTrashList.clear();
    }

    public String getPkgName() {
        return mPkgName;
    }

}
